
public class MyQueue {
    static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
        }
    }
    private Node head;
    private Node tail;
    //入队列
    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            tail = node;
        }
        else{
            tail.next = node;
            tail = tail.next;
        }
    }
    //获取队头元素
    public int peek(){
        if(head == null){
            throw new NotThing("队列为空!");
        }
        else{
            return head.val;
        }
    }
    //出队列
    public int poll(){
        if(head == null){
            throw new NotThing("队列为空!");
        }
        else if(head.next == null){
            int val = head.val;
            head = null;
            tail = null;
            return val;
        }
        else{
            int val = head.val;
            head = head.next;
            return val;
        }
    }
    //获取列有效元素个数
    public int size(){
        if(head == null){
            return 0;
        }
        else{
            int count = 0;
            Node cur = head;
            while(cur != null){
                cur = cur.next;
                count++;
            }
            return count;
        }
    }
    //检测队列是否为空
    public boolean isEmpty(){
        if(head == null){
            return true;
        }
        else{
            return false;
        }
    }

}

